package com.data.hijack.analysis.template;

import java.util.Map;
import java.util.Objects;

import org.apache.log4j.Logger;

import com.data.hijack.analysis.Proccessor;
import com.data.hijack.constant.Constants;
import com.data.hijack.util.StringUtils;

import cn.hutool.json.JSONUtil;

/**
 * @author husq
 */
public class TemplateProccessor implements Proccessor {
    private static final Logger logger = Logger.getLogger(TemplateProccessor.class);

    @Override
    public String proccess(String msg, Map<String, String> sysConfig, String bizName) throws Exception {
        String subkey = bizName + Constants.DOT;
        String msgTpKey = subkey + Constants.MSG_TYPE;
        String msgType = sysConfig.get(msgTpKey);
        String charsetKey = bizName + Constants.DOT + Constants.CHARSET;
        String charset = sysConfig.get(charsetKey);

        if (StringUtils.isBlank(msg)) {
            return "";
        }
        Object result = DataParser.parse(msg, sysConfig, bizName, msgType);

        String tranMsgTypeKey = subkey + Constants.TRANSFER_MSG_TYPE;
        String isTranslatableKey = subkey + Constants.IS_TRANSLATABLE;
        String modeKey = subkey + Constants.MODE;
        String tranMsgType = sysConfig.get(tranMsgTypeKey);
        String isTranslatable = sysConfig.get(isTranslatableKey);
        String mode = sysConfig.get(modeKey);
        String tranMsg = "";
        if (Objects.equals(mode, Constants.MODE_1) && Objects.equals(isTranslatable, Constants.IS_TRANSLATABLE_1)) {
            if (Objects.equals(tranMsgType, Constants.MSG_TYPE_JSON_DEF)) {
                tranMsg = JSONUtil.toJsonStr(JSONUtil.parseObj(result));
            }
        } else {
            tranMsg = result.toString();
        }

        return tranMsg;
    }
}
